<?php
final class EWebby_Runtime_Importer {
	public static function register() {
		spl_autoload_register(array('self', '_autoload'));
	}
	public static function unRegister() {
		spl_autoload_unregister(array('self', '_autoload'));
	}
	
	public static function Import($name) {
		$filePath = APP_PATH . str_replace('_', DIRECTORY_SEPARATOR, $name) . '.php';
		if( file_exists($filePath) ) {
			require_once($filePath);
			
			return true;
		}
		
		return false;
	}
	
    private static function _autoload($className) {
    	$result = self::Import($className);
    	if($result) {
    		$result = (class_exists($className, false) || interface_exists($className, false));
    	}
		
    	// on error, show runtime error
        if (!$result) {
	        eval( sprintf(
		        'class %s {
					function __construct() {
						throw new EWebby_Runtime_Exception(\'Class or Interface "%s" not found\');
					}
					static function __callstatic($m, $args) {
						throw new EWebby_Runtime_Exception(\'Class or Interface "%s" not found\');
					}
		        }', 
	        	$className,
	        	$className,
	        	$className) 
	        );
        }
    }
}